#coding=utf-8
from urllib import request
import re
import time
import os
import random

url='http://jandan.net/ooxx'
user_agent = r"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.6.2.15747"
header = {"User-Agent": user_agent}



shuliangreq=request.Request(url=url,headers=header)
shuliangcon=request.urlopen(shuliangreq).read().decode()
shuliangpattern=re.compile('<div id="comments">.*?<span class="current-comment-page">\[(.*?)\]</span>',re.S)
shuliang=re.findall(shuliangpattern,shuliangcon)

print(u'总共有',shuliang[0],u'页')

path = 'img'.strip()
isExists=os.path.exists(path)
if not isExists:
    print(u'偷偷新建了名字叫做',path,u'的文件夹')
    os.makedirs(path)
else:
    print(u'名为',path,u'的文件夹已经创建成功')

start = int(input(u"请输入起始页码\n"))
stop = int(input(u"请输入终止页码\n"))


ua_list=[r'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.6.2.15747',
            r'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
            r'Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3',
            r'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1']



for i in range(start,stop+1):
    iurl = url+'/page-'+str(i)+'#comments'
    print(iurl)
    req = request.Request(url=iurl, headers={'User-Agent':random.choice(ua_list)})
    con = request.urlopen(req)
    con_read = con.read().decode()
    pattern = re.compile('<div class="text".*?<p><a href="(.*?)".*?</li>',re.S)
    imglist=re.findall(pattern,con_read)
    print(u'本页有',len(imglist),u'张图片')
    # print(imglist)
    # img=request.urlopen('http:'+imglist[0]).read()
    for img in imglist:
        filename = 'img/'+img.split('/').pop()
        f=open(filename,'wb')
        f.write(request.urlopen(\
            request.Request(url='http:'+img,headers={'User-Agent':random.choice(ua_list)})\
        ).read()\
                )
        f.close()
        print("正在保存",filename)
        time.sleep(3)
